package com.sxapp.controller;

import java.io.File;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sxapp.util.DeleteFile;
import com.sxapp.util.FilePath;

@Controller
@RequestMapping("/image")
public class ImagesController {

	
	
	@RequestMapping("/updateIndexImages")
	@ResponseBody
	public int updateIndexImages(MultipartFile file,@RequestParam("imgname")String imgname) {
		System.out.println("ss");
		if (file!=null) {
			String imgName = file.getOriginalFilename();
			String lastImg = FilePath.images+imgname;
			if ((new File(lastImg)).exists()) {
				DeleteFile.deleteFile(lastImg);
			}
			FilePath.judgePath();
			try {
		      	//获取新图片的后缀名
		      	String suffixName = imgName.substring(imgName.lastIndexOf(".")+1);
		      	//获取老图片的名
		      	String prefixName = imgname.substring(0,imgname.lastIndexOf(".")+1);
		      	// 设置文件存储路径
		  	    String path = FilePath.images + prefixName+suffixName;
		      	File fileSource = new File(path);
		      	file.transferTo(fileSource);//文件写入
		  		return 1;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		return 0;
	}
}
